package com.tt.mymu.service.Impl;

import com.tt.mymu.service.ToolService;
import com.tt.mymu.utils.FtpUtil;
import com.tt.mymu.utils.IDUtils;
import com.tt.mymu.utils.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@Service
public class ToolServiceImpl implements ToolService {

    @Value("${tool.updateFile.img}")
    private String toolUpdateImg;
    @Value("${tool.updateFile.video}")
    private String toolUpdateVideo;

    @Value("${vsftpd.host}")
    private String host;
    @Value("${vsftpd.port}")
    private int port;
    @Value("${vsftpd.username}")
    private String username;
    @Value("${vsftpd.password}")
    private String password;
    @Value("${vsftpd.basePath}")
    private String basePath;
    @Value("${vsftpd.filePath}")
    private String filePath;
    @Value("${nginx.url}")
    private String url;

    /**
     * 上传 图片、视频 到本地 前端项目的静态文件夹
     *
     * @param file
     * @return
     */
    @Override
    public Result uploadFile(MultipartFile file) {
        try {
            // 获取上传的文件名
            String fileName = file.getOriginalFilename();
            //获取文件后缀
//            String substring = fileName.substring(fileName.lastIndexOf("."));
//            fileName= UUID.randomUUID()+substring;
            // 解析到文件后缀
            int index = fileName.lastIndexOf(".");
            String suffix = null;
            if (index == -1 || (suffix = fileName.substring(index + 1)).isEmpty()) {
                return Result.build(1, "文件后缀不能为空");
            }
            // 允许上传的文件后缀列表
            Set allowSuffix = new HashSet<>(Arrays.asList("jpg", "jpeg", "png", "gif"));
            if (!allowSuffix.contains(suffix.toLowerCase())) {
                // 如果不是图片就是视频
                file.transferTo(new File(toolUpdateVideo + fileName));
            } else {
                file.transferTo(new File(toolUpdateImg + fileName));
            }

            return Result.success();
        } catch (IOException e) {
            e.printStackTrace();
            return Result.build(1, "上传失败");

        }
    }

    /**
     * 删除 照片和视频
     *
     * @param filePath
     * @return
     */
    @Override
    public Result deleteFile(String filePath) {
        Path path = null;
        try {
            //path = Paths.get(toolUpdateImg + filePath);
            path = Paths.get(basePath + "/" + filePath);
            boolean b = Files.deleteIfExists(path);
            return Result.success(b);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.build(1, "删除失败");
        }
    }

    /**
     * 文件通过nginx上传到服务器
     * @param img
     * @return
     * @throws IOException
     */
    @Override
    public Map<String, Object> upload(MultipartFile img) throws IOException {
        Map<String, Object> map = new HashMap<>();
        //String filename = img.getOriginalFilename();
        String filename = IDUtils.genImageName()+img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf("."));
        boolean result = FtpUtil.uploadFile(host, port, username, password, basePath, filePath, filename, img.getInputStream());
        if (result) {
            map.put("url", url + "/" + filename);
        }
        return map;
    }


}
